<?php

//
// load a tab seperated text file as array
//
function load_tabbed_file($filepath, $load_keys=false){
    $array = array();
 
    if (!file_exists($filepath)){ return $array; }
    $content = file($filepath);

    for ($x=0; $x < count($content); $x++){
        if (trim($content[$x]) != ''){
            $line = explode("\t", trim($content[$x]));
            if ($load_keys){
                $key = array_shift($line);
                $array[$key] = $line;
            }
            else { $array[] = $line; }
        }
    }
    return $array;
}


/*
$products = array('riviste' =>
    array(
      array('id' => '001', 'image_src' => $path . 's_casa_facile.jpg' , 'description' => 'Casa Facile', 'price_label' => '€ 19,00', 'price' => 19 ),
      array('id' => '002', 'image_src' => $path . 'pc_professionale.jpg' , 'description' => 'PC Professionale', 'price_label' => '€ 25,00',  'price' => 25 ),
      array('id' => '003', 'image_src' => $path . 'casa_moderna.jpg' , 'description' => 'Casa Moderna', 'price_label' => '€ 25,00', 'price' => 25 ),
      array('id' => '004','image_src'  => $path . 'chi.jpg' , 'description' => 'Chi', 'price_label' => '€ 25,00', 'price' => 25 ),
      array('id' => '005','image_src'  => $path . 'panorama.jpg' , 'description' => 'Panorama', 'price_label' => '€ 25,00', 'price' => 25 ),
      array('id' => '006','image_src'  => $path . 'panoramauto.jpg' , 'description' => 'PanoramAuto', 'price_label' => '€ 25,00', 'price' => 25 )
      ), 
      
    'libri' => array(),
    'elettronica' => array(),
    'informatica' => array(),
    'profumeria' => array()
    
    // etc
      
     );
     
  */
  
    
        
// load the array from file
$loaded_array = load_tabbed_file('Matrice_Offerta_2013.06.23.txt');

// returns the array 
//echo "<pre>";  print_r($loaded_array); echo "</pre>";

$products = array();

$root = 'images/products/'; //cartella root delle immagini

$index =0;
foreach($loaded_array as $row){
	$category = strtolower(trim($row[3]));
	$category = str_replace(' ','-',$category);
	//prezzo in float
	$dec = filter_var($row[4], FILTER_SANITIZE_NUMBER_INT);
	$price = floatval($dec / 10);
	
	//$prod=array('id'=>str_pad($index,3,'0',STR_PAD_LEFT),  'description' => trim($row[6]), 'short_description' => trim($row[4]), 'price_label' => trim($row[3]), 'price' => $price, 'image_src' => $root.$category.'/'.trim($row[5]));
	$prod=array('id'=>str_pad($index,3,'0',STR_PAD_LEFT),  
				'description' => trim($row[7]), 
				'short_description' => trim($row[5]), 
				'price_label' => trim($row[4]), 
				'price' => $price, 
				'image_src' => $root.trim($row[6]),
				'gender'=>trim($row[1]),
				'tag'=>trim($row[2]));
	if(empty($prod['tag'])){
		$prod['tag']="elegante";
	}
	
	$products[$category][] = $prod;		
	$index ++;
}

//echo "<pre>";  print_r($products); echo "</pre>";
 



?>
                 